<?php

namespace plugins\store\controller;

use cmf\controller\ApiPluginBaseController;
use think\Db;
use plugins\store\validate\StoreValidate;
use plugins\store\model\StoreModel;
use plugins\store\model\StoreInfoModel;
use plugins\audit\model\AuditModel;
use plugins\store\model\ToolModel;
use app\common\lib\user_operate\PluginOperate;
use plugins\store\interfaceLib\StoreClassify;
use plugins\order\model\OrderModel;

class ApiIndexController extends ApiPluginBaseController
{
	public function index()
	{

	}

	//申请store
	public function apply_store()
	{
		$param = $this->request->post();

		//带有info的token
		$info = $this->getIdentity();
		//申请查重
		$check = ( new StoreModel )->get(['uid'=>$info->uid]);

		if($check){
			return api_json_echo( false , '您已拥有店铺或已在申请中，请勿重复操作' );
		}

		//无重复时将uid放入数组中
		$param['data']['uid'] = $info->uid;

		$result = $this->validate( $param['data'] , 'Api.applyStore');

		if(true !== $result){
			return api_json_echo( false , $result );
		}

		//检查有无审核模块
		$check = ( new PluginOperate )->isInstall('Audit','AuditStore');
		$store_model = new StoreModel();

		//创建时间生成
		$param['data']['create_time'] = time();
		$tool = new ToolModel();

		//store表log转换
		// $param['data']['store_log'] = $tool->base64_image_content($param['data']['store_log'],'log');

		$param['data']['store_log'] = fileUploadBase64($param['data']['store_log'],'store','log');

		//存入主信息
		$store_model->allowField(true)->save($param['data']);

		if($store_model->id){
		    $res=str_pad($store_model->id,4,"0",STR_PAD_LEFT);

		    //生成唯一的商店id
			$store_id = 'zy_'.$res.mt_rand(1000,9999);
			$store_model->save(['store_id'=>$store_id],['id'=>$store_model->id]);
			$store_info_model = new StoreInfoModel();
			$param['data']['store_id'] = $store_id;

			//手持身份证图片保存
			$param['data']['store_hand_idcard_img'] = fileUploadBase64($param['data']['store_hand_idcard_img'],'store','hand_idcard_img');

			//营业执照图片保存
			$param['data']['store_business_img'] = fileUploadBase64($param['data']['store_business_img'],'store','business_img');

			//身份证正面图片
			$param['data']['store_idcard_img_front'] = fileUploadBase64($param['data']['store_idcard_img_front'],'store','idcard_img_front');

			//身份证反面图片
			$param['data']['store_idcard_img_reverse'] = fileUploadBase64($param['data']['store_idcard_img_reverse'],'store','idcard_img_reverse');

			//info表保存
			$store_info_model->allowField(true)->save($param['data']);

			//判断有无审核
			if($check){
				//有审核的时候
				$audit = new AuditModel();
				//创建申请
				$audit->journal('store_id',$store_id);
			}else{
				//无审核的时候
				( new StoreClassify )->createStoreAdmin($store_id);
			}
			return api_json_echo( true , '店铺申请成功' );			
		}
		return api_json_echo( false , '店铺申请失败请重试' );
	}

	//store申请状态查询
	public function apply_check()
	{
		$post = $this->request->post();
		//带有info的token
		$info = $this->getIdentity();
		$model = ( new StoreModel )->get(['uid'=>$info->uid]);
		if($model){
			$status = $model->store_status;
			//店铺id
			$store_id = $model->store_id;
			$data = [];
			$data['type'] = $status;
			$message = ( new AuditModel )->order('time desc')->where(['store_id'=>$store_id])->find()->reason;
			if($data['type'] == 1){
				$message = '店铺正在审核中';
			}else if($data['type'] == 2){
				$message = '店铺已认证';
			}else if($data['type'] == 3){
				$message = '店铺审核未通过，请重新申请';
				$reason = ( new AuditModel )
						->order('time desc')
						->where(['store_id'=>$store_id])
						->field('reason')->find();
				$data['reason'] = $reason->reason;
			}else if($data['type'] == 4){
				$message = '店铺被禁用';
			}
			$data['message'] = $message;
			return api_json_echo( true , '查询成功' , $data );			
		}

		$data = [];
		$data['type'] = 0;
		$data['message'] = '申请店铺';
		return api_json_echo( true , '没有查询到申请信息' , $data);
	}


	//store重申请
	public function re_apply_store()
	{

		//返回上一次申请数据
		if($this->request->isGet()){
			$get = $this->request->get();

			//带有info的token
			$info = $this->getIdentity();
			$store = ( new StoreModel )->get(['uid'=>$info->uid]);
			if($store){
				if($store->store_status == 1){
					return api_json_echo( false , '查询失败，店铺正在审核中' );
				}else if($store->store_status == 3 || $store->store_status == 2){
					//驳回了,可以重新编辑
					$imghand =  zy_domain();
					//店铺信息
					$store_info = ( new StoreInfoModel )->get(['store_id'=>$store->store_id]);
					$array = [];
					$arr['store_name'] = $store['store_name'];
					$arr['store_principal_phone'] = $store['store_principal_phone'];
					$arr['store_principal'] = $store['store_principal'];
					$arr['store_type'] = $store['store_type'];
					$arr['store_hand_idcard_img'] = $imghand.$store_info['store_hand_idcard_img'];
					$arr['store_business_img'] = $imghand.$store_info['store_business_img'];
					$arr['store_idcard_img_front'] = $imghand.$store_info['store_idcard_img_front'];
					$arr['store_idcard_img_reverse'] = $imghand.$store_info['store_idcard_img_reverse'];
					$arr['store_idcard'] = $store_info['store_idcard'];
					$arr['store_desc'] = $store['store_desc'];
					$arr['store_log'] = $imghand.$store['store_log'];

					return api_json_echo( true , '查询成功' , $arr );

				}else{
					return api_json_echo( false , '查询失败，店铺状态未知' );
				}
			}

			return api_json_echo( false , '查询失败，未查询到店铺信息' );
		}

		//传入再次申请的数据，再次提交申请
		if($this->request->isPost()){
			$post = $this->request->post();
			//带有info的token
			$info = $this->getIdentity();
			$store = ( new StoreModel )->get(['uid'=>$info->uid]);
			if($store){
				if($store->store_status == 1){
					return api_json_echo( false , '修改失败，店铺正在审核中' );
				}else if($store->store_status == 3 || $store->store_status == 2){
					$post['data']['uid'] = $info->uid;
					$tool = new ToolModel();
					//店铺图标
					$post['data']['store_log'] = fileUploadBase64($post['data']['store_log'],'store','store_log');
					$store->allowField(true)->isUpdate(true)->save($post['data']);
					$store_info = ( new StoreInfoModel )->get(['store_id'=>$store->store_id]);
					//手持身份证图片保存
					$post['data']['store_hand_idcard_img'] = fileUploadBase64($post['data']['store_hand_idcard_img'],'store','hand_idcard_img');
					//营业执照图片保存
					$post['data']['store_business_img'] = fileUploadBase64($post['data']['store_business_img'],'store','business_img');
					//身份证正面图片
					$post['data']['store_idcard_img_front'] = fileUploadBase64($post['data']['store_idcard_img_front'],'store','idcard_img_front');
					//身份证反面图片
					$post['data']['store_idcard_img_reverse'] = fileUploadBase64($post['data']['store_idcard_img_reverse'],'store','idcard_img_reverse');
					$post['data']['store_status'] = 1;
					$result = $store_info->allowField(true)->isUpdate(true)->save($post['data']);
					if($result){
						$store->save(['store_status'=>1]);
						return api_json_echo( true , '修改成功' );
					}
						return api_json_echo( false , '修改失败' );
				}else{
					return api_json_echo( false , '修改失败，店铺状态未知' );
				}
			}

			return api_json_echo( false , '修改失败，未查询到店铺信息' );
		}
	}



    /**



     *查看审核情况



     */



    public function viewAudit($isModule=false)



    {  



        $param = $this->request->param();



        //是否存在



        $data = db( 'audit' )->where( 'store_id' , $param['store_id'])->order('time desc')->field('store_id,status,time,reason')->find();



        if(!$data) return zy_array (false,'请核实店铺id','',100,$isModule);



        return zy_array (true,'操作成功！',$data,200,$isModule);



    }	



    //店铺订单统计

	public function orderStatistics()

	{

		$post = $this->request->post();

		//带有info的token

		$info = $this->getIdentity();

		$store = ( new StoreModel )->get(['uid'=>$info->uid]);		

		if($store == null){

			return api_json_echo(false,'该账号下不存在店铺，请申请');

		}



		$store_id = $store->store_id;

		$data = [];

		$OrderModel = new OrderModel;

		//count
		$count = $OrderModel->count();

		//待付款count	1
		$data['obligation'] = $OrderModel->where(['order_status'=>1,'store_id'=>$store_id])->count();

		//待发货count	2
		$data['pending'] = $OrderModel->where(['order_status'=>2,'store_id'=>$store_id])->count();

		//已发货count	3

		$data['shipped'] = $OrderModel->where(['order_status'=>3,'store_id'=>$store_id])->count();

		//已完成count	4

		$data['finish'] = $OrderModel->where(['order_status'=>4,'store_id'=>$store_id])->count();



		//待退款

		$data['refund'] = $OrderModel->where(['order_refund'=>2,'store_id'=>$store_id])->count();



		//今日订单

		$data['today'] = $OrderModel

		->where(['order_status'=>['eq','1,2,3,4,5'],'store_id'=>$store_id,'submit_time'=>['between',[strtotime(date('Y-m-d', time())),strtotime(date('Y-m-d 23:59:59', time()))]]])

		->count();



		//已关闭count	5

		$count5 = $OrderModel->where(['order_status'=>5])->count();		



		return api_json_echo(true,'',$data);



	}





}







